<template>
  <div class="login">
    <!-- 可拖拽 -->
    <div class="drag"></div>
    <div class="content">
      <div class="left">
        <img
          src="../../assets/images/img2.png"
          class="people p-animtion"
          alt="people"
        />
        <img
          src="../../assets/images/img1.png"
          class="sphere s-animtion"
          alt="sphere"
        />
      </div>
      <div class="right">
        <div class="form-wrappepr">
          <h1>歪fChat</h1>
          <input
            type="text"
            class="inputs phone"
            placeholder="请输入您的手机号"
            v-model="userInfo.phone"
          />
          <input
            type="password"
            class="inputs pwd"
            placeholder="请输入您的密码"
            v-model="userInfo.password"
          />
          <input
            type="text"
            class="inputs nick"
            placeholder="请输入您的昵称"
            v-model="userInfo.nick"
          />
          <div class="tips">
            <div class="tips-wrap">
              已经有账号?<span @click="login">去登录</span>
            </div>
          </div>
          <button @click="register">注册</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
// 引入ts类型
import type { UserInfoData, RegisterResponse } from '@/api/login_register/type';
// 引入api接口
import { reqRegister } from '@/api/login_register/index';
import { ElMessage } from 'element-plus'
import { useRouter } from "vue-router";
const $router = useRouter();

onMounted(() => {
  document
    .querySelector(".people")
    ?.addEventListener("animationend", function () {
      (document.querySelector(".people") as any).classList.remove("p-animtion");
      (document.querySelector(".people") as any).classList.add(
        "p-other-animtion"
      );
    });
  document
    .querySelector(".sphere")
    ?.addEventListener("animationend", function () {
      (document.querySelector(".sphere") as any).classList.remove("s-animtion");
      (document.querySelector(".sphere") as any).classList.add(
        "s-other-animtion"
      );
    });
});

// 收集用户信息
let userInfo = ref<UserInfoData>({
  phone: "",
  password: "",
  nick: "",
});

// 注册按钮
const register = async() => {
  const { phone, password, nick } = userInfo.value;
  const phoneNumberRegex: RegExp = /^1[3456789]\d{9}$/;
  if(!phoneNumberRegex.test(phone)) {
    return ElMessage({
      type: 'warning',
      message: '请输入正确手机号'
    })
  } else if (!password) {
    return ElMessage({
      type: 'warning',
      message: '密码不能为空'
    })
  } else if (!nick) {
    return ElMessage({
      type: 'warning',
      message: '昵称不能为空'
    })
  } else if (nick.length > 15) {
    return ElMessage({
      type: 'warning',
      message: '昵称最大不能超过15个字'
    })
  }
  let res:RegisterResponse = await reqRegister(userInfo.value);
  if (res.status === 200) {
    ElMessage({
      type: 'success',
      message: '注册成功'
    })
    $router.push("/login");
  } else {
    ElMessage({
      type: 'error',
      message: res.msg
    })
  }
};

// 点击“去登录”提示
const login = () => {
  $router.push('/login')
}
</script>

<style scoped lang="scss">
@font-face {
  font-family: "AliFont";
  src: url("../../assets/iconfont/login.woff2") format("woff2"),
    url("../../assets/iconfont/login.woff") format("woff");
  font-display: swap;
}

:root {
  font-size: 15px;
}

.drag {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 30px;
  user-select: none;
  -webkit-app-region: drag;
}

.login {
  margin: 0;
  min-height: 100vh;
  background-color: #abc6f8;
  background-image: radial-gradient(
      closest-side,
      rgb(255, 255, 255),
      rgba(235, 105, 78, 0)
    ),
    radial-gradient(closest-side, rgb(250, 203, 203), rgba(243, 11, 164, 0)),
    radial-gradient(closest-side, rgb(237, 252, 202), rgba(254, 234, 131, 0)),
    radial-gradient(closest-side, rgb(197, 248, 241), rgba(170, 142, 245, 0)),
    radial-gradient(closest-side, rgb(206, 200, 243), rgba(248, 192, 147, 0));
  background-size: 130vmax 130vmax, 80vmax 80vmax, 90vmax 90vmax,
    110vmax 110vmax, 90vmax 90vmax;
  background-position: -80vmax -80vmax, 60vmax -30vmax, 10vmax 10vmax,
    -30vmax -10vmax, 50vmax 50vmax;
  background-repeat: no-repeat;
  animation: 10s movement linear infinite;
}

.login::after {
  content: "";
  display: block;
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.content {
  width: 90%;
  height: 90%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 1;
  border-radius: 30px;
  background: rgba(255, 255, 255, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.18);
  display: flex;
  .left {
    flex: 1;
    position: relative;
    .sphere {
      position: absolute;
      left: 30%;
      width: 90%;
      z-index: 1;
      animation: sphereAnimation 2s;
      animation-fill-mode: forwards;
      animation-timing-function: ease;
    }
    .people {
      position: absolute;
      left: -50%;
      top: 20%;
      width: 70%;
      // height: 100px;
      z-index: 2;
    }
    .p-animtion {
      animation: peopleAnimation 2s;
      animation-fill-mode: forwards;
      animation-timing-function: ease;
    }
    .p-other-animtion {
      animation-name: pOtherAnimation;
      animation-direction: alternate;
      animation-timing-function: linear;
      animation-iteration-count: infinite;
      animation-duration: 3s;
    }
    .s-animtion {
      animation: sphereAnimation 2s;
      animation-fill-mode: forwards;
      animation-timing-function: ease;
    }
    .s-other-animtion {
      animation-name: sOtherAnimation; // 动画名称
      animation-direction: alternate; // 动画在奇数次（1、3、5...）正向播放，在偶数次（2、4、6...）反向播放。
      animation-timing-function: linear; // 动画执行方式，linear：匀速；ease：先慢再快后慢；ease-in：由慢速开始；ease-out：由慢速结束；ease-in-out：由慢速开始和结束；
      animation-iteration-count: infinite; //  动画播放次数，infinite：一直播放
      animation-duration: 3s; // 动画完成时间
    }
  }
  .right {
    flex: 1;
    position: relative;
    z-index: 12;
    .top {
      width: 80%;
      margin-left: 38px;
      color: rgb(51, 52, 124);
      font-size: 20px;
      font-weight: 600;
      position: absolute;
      left: 50%;
      top: 5%;
      transform: translate(-50%, 0);
      .top-item {
        float: left;
        width: 150px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        margin-right: 10px;
        transition: 0.5s;
        &:hover {
          border: 0;
          background-color: #fff;
          border-radius: 50px;
          cursor: pointer;
          box-shadow: -20px 10px 32px 1px rgba(182, 183, 185, 0.37);
        }
      }
    }
    .form-wrappepr {
      width: 80%;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      text-align: right;
      h1 {
        float: left;
        margin: 30px 0;
        font-family: "AliFont";
        font-weight: 500;
        font-size: 40px;
      }
      .inputs {
        display: block;
        width: 100%;
        height: 50px;
        margin: 30px 0;
        border-radius: 10px;
        border: 0;
        background-color: rgb(210 223 237);
        color: rgb(80, 82, 84);
        outline: none;
        padding: 20px;
        box-sizing: border-box;
        font-size: 15px;
      }
      .tips {
        display: block;
        margin-top: -15px;
        color: rgb(160, 170, 182);
        cursor: pointer;
        font-size: 13px;
        .tips-wrap {
          width: 100%;
          display: flex;
          align-items: center;
          span {
            color: rgb(68, 96, 241);
          }
        }
      }
      button {
        width: 100%;
        height: 50px;
        background-color: rgb(68, 96, 241);
        border-radius: 10px;
        font-size: 15px;
        color: #fff;
        border: 0;
        font-weight: 600;
        margin: 30px 0;
        cursor: pointer;
        box-shadow: -20px 28px 42px 0 rgba(62, 145, 255, 0.37);
      }
    }
  }
}

@keyframes movement {
  0%,
  100% {
    background-size: 130vmax 130vmax, 80vmax 80vmax, 90vmax 90vmax,
      110vmax 110vmax, 90vmax 90vmax;
    background-position: -80vmax -80vmax, 60vmax -30vmax, 10vmax 10vmax,
      -30vmax -10vmax, 50vmax 50vmax;
  }
  25% {
    background-size: 100vmax 100vmax, 90vmax 90vmax, 100vmax 100vmax,
      90vmax 90vmax, 60vmax 60vmax;
    background-position: -60vmax -90vmax, 50vmax -40vmax, 0vmax -20vmax,
      -40vmax -20vmax, 40vmax 60vmax;
  }
  50% {
    background-size: 80vmax 80vmax, 110vmax 110vmax, 80vmax 80vmax,
      60vmax 60vmax, 80vmax 80vmax;
    background-position: -50vmax -70vmax, 40vmax -30vmax, 10vmax 0vmax,
      20vmax 10vmax, 30vmax 70vmax;
  }
  75% {
    background-size: 90vmax 90vmax, 90vmax 90vmax, 100vmax 100vmax,
      90vmax 90vmax, 70vmax 70vmax;
    background-position: -50vmax -40vmax, 50vmax -30vmax, 20vmax 0vmax,
      -10vmax 10vmax, 40vmax 60vmax;
  }
}
@keyframes sphereAnimation {
  0% {
    width: 10%;
  }
  100% {
    width: 90%;
    transform: translate(-30%, 5%);
  }
}
@keyframes peopleAnimation {
  0% {
    width: 40%;
  }
  100% {
    width: 70%;
    transform: translate(90%, -10%);
  }
}

@keyframes pOtherAnimation {
  0% {
    transform: translate(90%, -10%);
  }
  100% {
    transform: translate(90%, -15%);
  }
}
@keyframes sOtherAnimation {
  0% {
    transform: translate(-30%, 5%);
  }
  100% {
    transform: translate(-30%, 10%);
  }
}
</style>
